-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[DAG] visitTRUNCATE - early out from computeKnownBits/ComputeNumSignBits failures. NFC. #154111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…its failures. NFC. Avoid unnecessary (costly) computeKnownBits/ComputeNumSignBits calls - use MaskedValueIsZero instead of computeKnownBits directly to simplify code.
@llvm/pr-subscribers-llvm-selectiondag Author: Simon Pilgrim (RKSimon) ChangesAvoid unnecessary (costly) computeKnownBits/ComputeNumSignBits calls - use MaskedValueIsZero instead of computeKnownBits directly to simplify code. Full diff: https://github.com/llvm/llvm-project/pull/154111.diff 1 Files Affected:
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 785245b2d9e74..0e63f051b1d65 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -16332,25 +16332,22 @@ SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
// (trunc (abdu/abds a, b)) -> (abdu/abds (trunc a), (trunc b))
if ((!LegalOperations || N0.hasOneUse()) &&
TLI.isOperationLegal(N0.getOpcode(), VT)) {
- EVT SrcVT = N0.getValueType();
EVT TruncVT = VT;
unsigned SrcBits = SrcVT.getScalarSizeInBits();
unsigned TruncBits = TruncVT.getScalarSizeInBits();
- unsigned NeededBits = SrcBits - TruncBits;
SDValue A = N0.getOperand(0);
SDValue B = N0.getOperand(1);
bool CanFold = false;
if (N0.getOpcode() == ISD::ABDU) {
- KnownBits KnownA = DAG.computeKnownBits(A);
- KnownBits KnownB = DAG.computeKnownBits(B);
- CanFold = KnownA.countMinLeadingZeros() >= NeededBits &&
- KnownB.countMinLeadingZeros() >= NeededBits;
+ APInt UpperBits = APInt::getBitsSetFrom(SrcBits, TruncBits);
+ CanFold = DAG.MaskedValueIsZero(A, UpperBits) &&
+ DAG.MaskedValueIsZero(B, UpperBits);
} else {
- unsigned SignBitsA = DAG.ComputeNumSignBits(A);
- unsigned SignBitsB = DAG.ComputeNumSignBits(B);
- CanFold = SignBitsA > NeededBits && SignBitsB > NeededBits;
+ unsigned NeededBits = SrcBits - TruncBits;
+ CanFold = DAG.ComputeNumSignBits(A) > NeededBits &&
+ DAG.ComputeNumSignBits(B) > NeededBits;
}
if (CanFold) {
|
CanFold = DAG.MaskedValueIsZero(A, UpperBits) && | ||
DAG.MaskedValueIsZero(B, UpperBits); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CanFold = DAG.MaskedValueIsZero(A, UpperBits) && | |
DAG.MaskedValueIsZero(B, UpperBits); | |
CanFold = DAG.MaskedValueIsZero(B, UpperBits) && | |
DAG.MaskedValueIsZero(A, UpperBits); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why to check the second oprand for ABDU/ABDS firstly, in my understadning, the second is smaller, the possibility of MaskedValueIsZero will be bigger.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for commutable instructions we move constants to the RHS, in the middle-end the node is often canonicalized to the RHS being the simpler in other ways as well but we don't tend to bother as much in DAG.
CanFold = DAG.ComputeNumSignBits(A) > NeededBits && | ||
DAG.ComputeNumSignBits(B) > NeededBits; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CanFold = DAG.ComputeNumSignBits(A) > NeededBits && | |
DAG.ComputeNumSignBits(B) > NeededBits; | |
CanFold = DAG.ComputeNumSignBits(B) > NeededBits && | |
DAG.ComputeNumSignBits(A) > NeededBits; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Avoid unnecessary (costly) computeKnownBits/ComputeNumSignBits calls - use MaskedValueIsZero instead of computeKnownBits directly to simplify code.